/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.magnifier;

import java.awt.Color;
import java.beans.PropertyChangeEvent;

import twaver.Element;
import twaver.Node;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.chart.PercentChart;
import twaver.network.ui.ComponentAttachment;
import twaver.network.ui.ElementUI;

public class ServerAttachment extends ComponentAttachment{
	private TDataBox box;
	private PercentChart healthChart;
	private Element cpu;
	private Element memory;
	private Element traffic;
	
	public ServerAttachment(String name, ElementUI ui) {
		super(name, ui);
		
		box = new TDataBox();
		cpu = createElement("CPU", 0, TWaverConst.PERCENT_STYLE_PLANE);
		memory = createElement("Memory", 0, TWaverConst.PERCENT_STYLE_PLANE);
		traffic = createElement("Traffic", 0, TWaverConst.PERCENT_STYLE_PLANE);
		box.addElement(cpu);
		box.addElement(memory);
		box.addElement(traffic);
		healthChart = new PercentChart(box){
			public String getPercentLabel(Element element) {
				return getLegendLabel(element);
			}
			public Color getPercentColor(Element element) {
				return getCharColor(element.getChartValue());
			}
		};		
		healthChart.setGradient(true);
		healthChart.getLegendPane().setVisible(false);
	    healthChart.setPercentLabelColor(new Color(244,244,244));
		healthChart.setOpaque(false);
		
		this.setComponent(healthChart);
		this.setArc(8);
		this.setShrinkable(true);
		this.setStyle(TWaverConst.ATTACHMENT_STYLE_BUBBLE);
		this.setPosition(TWaverConst.POSITION_LEFT);
		this.setDirection(TWaverConst.ATTACHMENT_DIRECTION_TOP_LEFT);
		this.setBorderVisible(true);
		this.setBorderColor(Color.BLACK);
		this.setBodyVisible(true);
		this.setBodyGradient(true);
		this.setBodyColor(new Color(0, 0, 0, 125));
		this.setBodyGradientColor(new Color(255, 255, 255, 125));
		this.setWidth(120);
		this.setHeight(70);
		this.setXOffset(-2);
		this.setYOffset(5);
	}

	public void elementPropertyChange(PropertyChangeEvent evt){
		super.elementPropertyChange(evt);
		String propertyName = evt.getPropertyName();
		if(!(propertyName.equals(TWaverConst.CLIENT_PROPERTY_PREFIX+"cpu")||
				propertyName.equals(TWaverConst.CLIENT_PROPERTY_PREFIX+"memory")||
				propertyName.equals(TWaverConst.CLIENT_PROPERTY_PREFIX+"traffic"))){
			return ;
		}
		Object cpuValue = this.element.getClientProperty("cpu");
		if(cpuValue instanceof Double){
			double doubleValue = ((Double)cpuValue).doubleValue();
			cpu.putChartValue(doubleValue);
		}
		Object memoryValue = this.element.getClientProperty("memory");
		if(memoryValue instanceof Double){
			double doubleValue = ((Double)memoryValue).doubleValue();
			memory.putChartValue(doubleValue);
		}
		Object trafficValue = this.element.getClientProperty("traffic");
		if(trafficValue instanceof Double){
			double doubleValue = ((Double)trafficValue).doubleValue();
			traffic.putChartValue(doubleValue);
		}
	}

	private Color getCharColor(double doubleValue) {
		int r = 100, g = 100;
		r = (int) (255d * doubleValue / 100);
		if (r > 230) {
			r = 255;
		} else if (r <= 25) {
			r = 0;
		}
		g = 255 - r;
		Color color = new Color(r, g, 0);
		return color;
	}

	private Element createElement(String name, double current, int style) {
		Element element = new Node();
		element.putChartPercentSpareFill(true);
		element.setName(name);
		element.putChartValue(current);
		element.putChartPercentStyle(style);
		return element;
	}
}